'use strict'
const path = require('path')
const fs = require('fs')
// const defaultSettings = require('./src/settings.ts')
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')

//压缩开始
const CompressionPlugin = require('compression-webpack-plugin')
//压缩结束

const isProdOrTest = process.env.NODE_ENV !== 'development'

// const FileManagerPlugin = require('filemanager-webpack-plugin')


function resolve(dir) {
  return path.join(__dirname, dir)
}
const timeUpdate  = new Date().getTime();//当前打包时间作为版本号
// 标题设置
const systemTitle = "寰宇青岛智能工厂"

//端口号设置
const port = process.env.port || process.env.npm_config_port || 9527 // dev port

// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
  productionSourceMap: false,
  pages: {
      index: {
        // page 的入口
        entry: './src/main',
      },
  },
  //发布的地址，会在url后面加上该地址，相当于ContextPath
  publicPath: process.env.NODE_ENV === 'development' ? '' : '',
  //打包输出目录
  outputDir: 'dist',
  //静态资源的输出目录
  assetsDir: 'static',
  // 运行时编译 antv x6 vue渲染的场合需要
  runtimeCompiler: true,
  //设置是否在开发环境下每次保存代码时都启用 eslint验证。
  lintOnSave: process.env.NODE_ENV === 'development' ? false : false,
  productionSourceMap: false,
  // https 模式
  devServer: {
    port: port,
  
    open: true,
    // https: false,
    // 开启的端口可以通过https访问
    // https: {
    //   key: fs.readFileSync('./cert/qdhz.0tool.com.key'),
    //   cert: fs.readFileSync('./cert/qdhz.0tool.com.crt')
    // },
    proxy: {
      '/lxsh-cer-web-pc/': {
        // target: 'http://localhost:10083',
        // target: 'http://localhost:9090',
         // target: 'http://dacheng.etlink.top:72/',
         target: 'https://qdhz.0tool.com:73/',
         changeOrigin: true,
         secure: false,
         pathRewrite: {
           ['lxsh-auth-web-pc']: 'lxsh-cer-web-pc'
         }
       },
      [process.env.VUE_APP_BASE_API]: {
        // target: 'http://localhost:8383',
        target: 'http://192.168.2.92:81/',
        // target: 'http://localhost:9090',
        // target: 'http://dacheng.etlink.top:72/',
        // target: 'http://10.33.91.11:9090',
        // target: 'http://192.168.2.207:9090/',
        // target: 'http://192.168.2.90:18180/',
        changeOrigin: true,
        secure: false
        // pathRewrite: {
        //   ['^' + process.env.VUE_APP_BASE_API]: ''
        // }
      }
    },
  },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: systemTitle,
    //别名
    resolve: {
      alias: {
        'lxsh-cer-web-pc/src': resolve('./src')
      }
    },
    plugins: [
      new NodePolyfillPlugin(),
      // new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
      
      // new MonacoWebpackPlugin()
    ],
    output: {
      filename: `static/js/[name].js?v=${timeUpdate}`,
      chunkFilename: `static/js/[name].js?v=${timeUpdate}`
    },
  },
  
  chainWebpack(config) {

    if(isProdOrTest){//生产环境或测试环境下

      config.plugin('compressionPlugin').use(
        // 下面两项配置才是 compression-webpack-plugin 压缩配置
        // 压缩成 .gz 文件
        new CompressionPlugin({
          filename: '[path][base].gz[query]',// 压缩后的文件名
          algorithm: 'gzip',
          test: /\.js$|\.html$|\.css$/, // 匹配文件名
          threshold: 10240,
          minRatio: 0.8,
          deleteOriginalAssets: true // 是否删除原资源
        }),
      );
    }


    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()

    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config.module
      .rule('bpmnlintrc')
      .test(/\.bpmnlintrc$/)
      .use('bpmnlint-loader')
      .loader('bpmnlint-loader')
      .end()

    // config.module.rule("typescript").test(/\.ts$/).use("ts-loader").loader("ts-loader").end()

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          // config
          //   .plugin('ScriptExtHtmlWebpackPlugin')
          //   .after('html')
          //   .use('script-ext-html-webpack-plugin', [{
          //     // `runtime` must same as runtimeChunk name. default is `runtime`
          //     inline: /runtime\..*\.js$/
          //   }])
          //   .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
          config.optimization.runtimeChunk('single')
        }
      )
  }
}
